//
//  APOUser.m
//  Apallo
//
//  Created by Jason Poh on 20/8/14.
//  Copyright (c) 2014 Apallo. All rights reserved.
//

#import "APOUser.h"
#import "APOSaveUtility.h"

@implementation APOUser

NSString *const APOUserToken = @"APOUserToken";
NSString *const APOUserIsLockApp = @"APOUserIsLockApp";
NSString *const APOUserAppPassword = @"APOUserAppPassword";
NSString *const APOUserStudentDetails = @"APOStudentDetails";
NSString *const APOUserIsSuccessfullyLoggedIn = @"APOUserIsSuccessfullyLoggedIn";

static APOUser *user;

+(APOUser*) getUser
{
    if(user == nil) {
        NSLog(@"User object is nil, init new user");
        user = [[super allocWithZone:NULL] init];
    }
    return user;
        
}

+(void) setUser: (APOUser*)userObj
{
    user = userObj;
    if(user != nil) {
        [user debugPrint];
        [[user student_details] debugPrint];
    }else {
        NSLog(@"APOUser - setUser: user is nil");
    }
        
    
}

-(void) debugPrint
{
    NSLog(@"APOUser - token: %@", self.token);
    NSLog(@"APOUser - isSuccessfullyLoggedIn: %d", self.isSuccessfullyLoggedIn);
    NSLog(@"APOUser - isLockApp: %d", self.isLockApp);
    NSLog(@"APOUser - app_password: %@", self.app_password);
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.token forKey:APOUserToken];
    [coder encodeBool:self.isLockApp forKey:APOUserIsLockApp];
    [coder encodeObject:self.app_password forKey:APOUserAppPassword];
    [coder encodeObject:self.student_details forKey:APOUserStudentDetails];
    [coder encodeBool:self.isSuccessfullyLoggedIn forKey:APOUserIsSuccessfullyLoggedIn];

}

- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        _token = [coder decodeObjectForKey:APOUserToken];
        _isLockApp = [coder decodeBoolForKey:APOUserIsLockApp];
        _app_password = [coder decodeObjectForKey:APOUserAppPassword];
        _student_details = [coder decodeObjectForKey:APOUserStudentDetails];
        _isSuccessfullyLoggedIn = [coder decodeBoolForKey:APOUserIsSuccessfullyLoggedIn];
        
    }
    return self;
}


- (id)init
{
    if ( (self = [super init]) ) {
        self.token = @"";
        self.student_details = [[APOStudent alloc] init];
        self.isLockApp = false;
        self.app_password = @"";
    }
    return self;
}

@end
